﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;

namespace YArchitech.Controls
{
	public class MostValueConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				MostValue mostValue = (MostValue)value;
				return mostValue.Minimal.ToString() + "," + mostValue.Maximum.ToString();
			}
			if (destinationType == typeof(InstanceDescriptor))
			{
				MemberInfo constructor = typeof(MostValue).GetConstructor(new Type[]
				{
					typeof(double),
					typeof(double)
				});
				MostValue mostValue2 = (MostValue)value;
				return new InstanceDescriptor(constructor, new object[]
				{
					mostValue2.Minimal,
					mostValue2.Maximum
				});
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (!(value is string))
			{
				return base.ConvertFrom(context, culture, value);
			}
			string[] array = ((string)value).Split(new char[]
			{
				','
			});
			if (array.GetLength(0) != 2)
			{
				throw new ArgumentException("Invalid\u00a0parameter\u00a0format");
			}
			return new MostValue
			{
				Minimal = Convert.ToDouble(array[0]),
				Maximum = Convert.ToDouble(array[1])
			};
		}
	}
}
